package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.entity.Elder;
import com.ruoyi.system.mapper.ElderMapper;
import com.ruoyi.system.service.IElderService;
import com.ruoyi.system.service.IRoomService;
import com.ruoyi.system.service.impl.RoomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/elder")
public class ElderController extends BaseController {

    @Autowired
    private IElderService elderService;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private IRoomService roomService;

    @GetMapping("/list")
    public AjaxResult list(Elder elder) {
        List<Elder> list = elderService.list(Wrappers.query(elder));
        return AjaxResult.success(list);
    }

    @GetMapping("/byRoom/{roomId}")
    public AjaxResult listByRoomId(@PathVariable Long roomId) {

        List<Elder> list = elderMapper.selectByRoomId(roomId);
        return AjaxResult.success(list);
    }

    @PostMapping("/save/{roomId}")
    public AjaxResult saveToRoom(@PathVariable Long roomId,@RequestBody Elder elder) {
        roomService.addToRoom(roomId, elder);
        return AjaxResult.success();
    }


    @PostMapping
    public AjaxResult add(@RequestBody Elder elder) {
        elderService.save(elder);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Elder elder) {
        elderService.updateById(elder);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //elderService.removeById(id);
        elderMapper.deleteById(id);
        return AjaxResult.success();
    }
}